#pragma once

#include "../can_adapter.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/EventLoopThread.h"

#include <atomic>
#include <map>
namespace sensor::radar {
class CanTransreceiver;
class ChuansuCanAdapter : public CanAdapter {
public:
    explicit ChuansuCanAdapter(std::unordered_map<std::string, std::shared_ptr<CanTransreceiver>> devs);

    virtual void onReceiveCanMessage(const std::string& dev, int fid, muduo::net::Buffer& buffer) override;
    virtual void onReceiveGnssMessage(const interface::msg::Gnss::SharedPtr msg) override;
    virtual void onReceiveInformationMessage(const interface::msg::CarInformation msg) override;
    double degreesToRadians(double degrees);
    std::string printObject(const interface::msg::Object& object);

private:
    void onSendRadar1Handler();
    void onSendRadar2Handler();

    void onReceiveMR418QualityInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveMR418ExtendInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveMR418GeneralInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveMR418TrackListStatus(const std::string& dev, int fid, muduo::net::Buffer& buffer);

    // 通过不同的帧ID 来判别角雷达安装位置
    void onReceiveSR439ExtendFLInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439ExtendFRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439ExtendRLInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439ExtendRRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);

    void onReceiveSR439QualityFLInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439QualityFRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439QualityRLInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439QualityRRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);

    void onReceiveSR439GeneralFLInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439GeneralFRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439GeneralRLInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiveSR439GeneralRRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);

    void onReceiveSR439TrackListStatus(const std::string& dev, int fid, muduo::net::Buffer& buffer);

private:
    using CanMessageUnpackFunction = std::function<void(const std::string& dev, int fid, muduo::net::Buffer& buffer)>;
    std::map<int, CanMessageUnpackFunction> message_unpack_table_;
    static std::unordered_map<int, int> chuansuObjectTypeMap;
    std::mutex radar1MapLock_;
    std::mutex radar2MapLock_;
    std::mutex radar3MapLock_;
    std::mutex radar4MapLock_;
    std::map<int, std::pair<uint64_t, interface::msg::Object>> radar1ObjectsMap;
    std::map<int, std::pair<uint64_t, interface::msg::Object>> radar2ObjectsMap;
    std::map<int, std::pair<uint64_t, interface::msg::Object>> radar3ObjectsMap;
    std::map<int, std::pair<uint64_t, interface::msg::Object>> radar4ObjectsMap;
    std::shared_ptr<muduo::net::EventLoopThread> radar1LoopThread_ { nullptr };
    std::shared_ptr<muduo::net::EventLoopThread> radar2LoopThread_ { nullptr };
};

}